from .broker import Broker
from .dpin import DPin
from .ain import Ain
from .aout import Aout

import time
import struct




class COSA:
    def __init__(self, broker):
        self._b = broker

    @property
    def power(self):
        return 0

    @power.setter
    def power(self, val):
        while not self._b.cdb1_idle():
            pass

        self._b.twi_sbw(126, b'\x00\x9f')
        cmd = bytearray(b'\x80\x00\x00\x00\x04\x00\x00\x00\x0f\x0a\x01\x00')
        if val:  # power on
            cmd[-1] = 1
        else:
            cmd[-1] = 0
        cmd[133-128] = self._b.cdb_chkcode(cmd)
        self._b.twi_sbw(130, cmd[2:])
        self._b.twi_sbw(128, cmd[:2])
        while self._b.cdb1_cip():
            pass

  

